﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Library;
using System.Data;
using IHISCETeim.BLL;
using System.Text;


namespace IHISCETeim.Web.MachineSys.FTradeModel
{
    public partial class FTradeCatheringListEasyUI2 : EasyUIPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "FTradeCatheringListEasyUI";
            }
        }



        protected string GetYearList()
        {
            string liTemplete = "<option value='{0}' {1}>{0}年</option>";
            string rHtml = "";
            int nowYear = DateTime.Now.Year;
            for (int i = (nowYear - 3); i <= nowYear; i++)
            {
                rHtml = rHtml + string.Format(liTemplete, i, i == Year ? "selected" : "");
            }
            return rHtml;
        }


        #region 
        protected int Year {
            get {
                return GetRequestValue<int>("year", DateTime.Now.Year);
            }
        }
        protected int Month
        {
            get
            {
                return GetRequestValue<int>("Month", DateTime.Now.Month);
            }
        }
        /// <summary>
        /// 所选月份第一天
        /// </summary>
        protected DateTime FDate
        {
            get
            {
                return Convert.ToDateTime(string.Format("{0}-{1}-{2}",Year,Month,1));
            }
        }
        /// <summary>
        /// 所选月份最后一天
        /// </summary>
        protected DateTime TDate
        {
            get
            {
                return FDate.AddMonths(1).AddDays(-1);
            }
        }

        protected DateTime YearFDate
        {
            get
            {
                return Convert.ToDateTime(FDate.Year.ToString() + "-01-01");
            }
        }
        protected DateTime YearTDate
        {
            get
            {
                return Convert.ToDateTime(FDate.Year.ToString() + "-12-31");
            }
        }
        #endregion
        /*
         获取表格列标题_合并第一行标题
         */
        protected string GetColumnsJS()
        {
            DateTime everyday = FDate;
            DateTime weekStateDay = FDate;
            int weekcount = 1;//周计数
            string returnstr = "";
            while (everyday <= TDate)
            {
                if ((everyday.DayOfWeek == DayOfWeek.Sunday) || (everyday == TDate))
                {
                    returnstr += string.Format("{{title:'第 {0} 周({1})',colspan:5}},", weekcount, weekStateDay.ToString("MM.dd") + "-" + everyday.ToString("MM.dd"));

                    weekStateDay = everyday.AddDays(1);
                    weekcount++;
                }
                everyday = everyday.AddDays(1);
            }
            returnstr += string.Format("{{title:'{0}',colspan:5}},"," 本月合计 ");
            returnstr += string.Format("{{title:'{0}',colspan:5}}"," 本年累计 ");
            return returnstr;
        }
        /// <summary>
        /// 取表格列标题_合并第二行标题
        /// </summary>
        /// <returns></returns>
        protected string GetColumnsJS2()
        {
            DateTime everyday = FDate;
            DateTime weekStateDay = FDate;
            int weekcount = 1;//周计数
            StringBuilder returnstr = new StringBuilder() ;
            while (everyday <= TDate)
            {
                if ((everyday.DayOfWeek == DayOfWeek.Sunday) || (everyday == TDate))
                {
                    //{ title: '电汇', field: '电汇_2014-02-01_2014-02-07', align:'center', width: 90,formatter:milliformatter }
                    returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }},", "电汇", weekStateDay.ToString("yyyy-MM-dd"), everyday.ToString("yyyy-MM-dd"));
                    returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }},", "汇票", weekStateDay.ToString("yyyy-MM-dd"), everyday.ToString("yyyy-MM-dd"));
                    returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }},", "融资", weekStateDay.ToString("yyyy-MM-dd"), everyday.ToString("yyyy-MM-dd"));
                    returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }},", "其他", weekStateDay.ToString("yyyy-MM-dd"), everyday.ToString("yyyy-MM-dd"));
                    returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }},", "合计", weekStateDay.ToString("yyyy-MM-dd"), everyday.ToString("yyyy-MM-dd"));


                    weekStateDay = everyday.AddDays(1);
                    weekcount++;
                }
                everyday = everyday.AddDays(1);
            }

            //本月合计
            returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }},", "电汇", FDate.ToString("yyyy-MM-dd"), TDate.ToString("yyyy-MM-dd"));
            returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }},", "汇票", FDate.ToString("yyyy-MM-dd"), TDate.ToString("yyyy-MM-dd"));
            returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }},", "融资", FDate.ToString("yyyy-MM-dd"), TDate.ToString("yyyy-MM-dd"));
            returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }},", "其他", FDate.ToString("yyyy-MM-dd"), TDate.ToString("yyyy-MM-dd"));
            returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }},", "合计", FDate.ToString("yyyy-MM-dd"), TDate.ToString("yyyy-MM-dd"));

            //本年合计
            returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }},", "电汇", YearFDate.ToString("yyyy-MM-dd"), YearTDate.ToString("yyyy-MM-dd"));
            returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }},", "汇票", YearFDate.ToString("yyyy-MM-dd"), YearTDate.ToString("yyyy-MM-dd"));
            returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }},", "融资", YearFDate.ToString("yyyy-MM-dd"), YearTDate.ToString("yyyy-MM-dd"));
            returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }},", "其他", YearFDate.ToString("yyyy-MM-dd"), YearTDate.ToString("yyyy-MM-dd"));
            returnstr.AppendFormat("{{ title: '{0}', field: '{0}_{1}_{2}', align:'center', width: 90,formatter:hrefClickProc }}", "合计", YearFDate.ToString("yyyy-MM-dd"), YearTDate.ToString("yyyy-MM-dd"));

            //returnstr += string.Format("{{title:'{0}',colspan:5}},", " 本月合计 ");
            //returnstr += string.Format("{{title:'{0}',colspan:5}},", " 本年累计 ");
            return returnstr.ToString();
        }

        //protected override void OnLoad(EventArgs e)
        //{
        //    base.OnLoad(e);
        //    GridView1.datat
        //}

        protected override string query()
        {
            //营业经理权限处理
            string agentsqltemp = base.FTradeAgentPowerSqlWhere();
            //
            DataTable JingXiaoShangDT = FTradeCatheringController.GetHuKunAgentList(agentsqltemp, YearFDate.ToString("yyyy-MM-dd 00:00:00"), YearTDate.ToString("yyyy-MM-dd 23:59:59"));
            ////加入合计行
            //DataRow hjDR = JingXiaoShangDT.NewRow();
            //hjDR["JingXiaoShang"]=
            //
            DateTime everyday = FDate;
            DateTime weekStateDay = FDate;
            int weekcount = 1;//周计数
            StringBuilder returnstr = new StringBuilder();
            while (everyday <= TDate)
            {
                if ((everyday.DayOfWeek == DayOfWeek.Sunday) || (everyday == TDate))
                {
                    JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "电汇", weekStateDay.ToString("yyyy-MM-dd"), everyday.ToString("yyyy-MM-dd")), typeof(decimal));
                    JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "汇票", weekStateDay.ToString("yyyy-MM-dd"), everyday.ToString("yyyy-MM-dd")), typeof(decimal));
                    JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "融资", weekStateDay.ToString("yyyy-MM-dd"), everyday.ToString("yyyy-MM-dd")), typeof(decimal));
                    JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "其他", weekStateDay.ToString("yyyy-MM-dd"), everyday.ToString("yyyy-MM-dd")), typeof(decimal));
                    JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "合计", weekStateDay.ToString("yyyy-MM-dd"), everyday.ToString("yyyy-MM-dd")), typeof(decimal));
                   

                    weekStateDay = everyday.AddDays(1);
                    weekcount++;
                }
                everyday = everyday.AddDays(1);
            }

            //本月合计
            JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "电汇", FDate.ToString("yyyy-MM-dd"), TDate.ToString("yyyy-MM-dd")), typeof(decimal));
            JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "汇票", FDate.ToString("yyyy-MM-dd"), TDate.ToString("yyyy-MM-dd")), typeof(decimal));
            JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "融资", FDate.ToString("yyyy-MM-dd"), TDate.ToString("yyyy-MM-dd")), typeof(decimal));
            JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "其他", FDate.ToString("yyyy-MM-dd"), TDate.ToString("yyyy-MM-dd")), typeof(decimal));
            JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "合计", FDate.ToString("yyyy-MM-dd"), TDate.ToString("yyyy-MM-dd")), typeof(decimal));

            //本年合计
            JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "电汇", YearFDate.ToString("yyyy-MM-dd"), YearTDate.ToString("yyyy-MM-dd")), typeof(decimal));
            JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "汇票", YearFDate.ToString("yyyy-MM-dd"), YearTDate.ToString("yyyy-MM-dd")), typeof(decimal));
            JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "融资", YearFDate.ToString("yyyy-MM-dd"), YearTDate.ToString("yyyy-MM-dd")), typeof(decimal));
            JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "其他", YearFDate.ToString("yyyy-MM-dd"), YearTDate.ToString("yyyy-MM-dd")), typeof(decimal));
            JingXiaoShangDT.Columns.Add(string.Format("{0}_{1}_{2}", "合计", YearFDate.ToString("yyyy-MM-dd"), YearTDate.ToString("yyyy-MM-dd")), typeof(decimal));

            ////数据添加
            foreach(DataRow dr in JingXiaoShangDT.Rows)
            {
                string JingXiaoShang = dr["JingXiaoShang"].ToString();
                foreach (DataColumn dc in JingXiaoShangDT.Columns)
                {
                    if (dc.ColumnName.ToLower() != "jingxiaoshang")
                      dr[dc]=GetVal(JingXiaoShang,dc.ColumnName.ToLower());
                }
            }
            //2.处 理合计列
            DataTable hjDT = new DataTable();
            foreach (DataColumn dc in JingXiaoShangDT.Columns)
            {
                hjDT.Columns.Add(dc.ColumnName);
            }
            hjDT.Rows.Add(hjDT.NewRow());
            foreach (DataRow dr in hjDT.Rows)
            {
                string JingXiaoShang ="合计:";
                foreach (DataColumn dc in hjDT.Columns)
                {
                    if (dc.ColumnName.ToLower() != "jingxiaoshang")
                        dr[dc] = GetVal(JingXiaoShang, dc.ColumnName.ToLower());
                }
            }


            return ToDataGridJson(JingXiaoShangDT, JingXiaoShangDT.Rows.Count, hjDT).ToString(); ;
        }
        /// <summary>
        /// 获取各个列值 
        /// </summary>
        /// <param name="JingXiaoShang"></param>
        /// <param name="ColumnName"></param>
        /// <returns></returns>
        private object GetVal(string JingXiaoShang,string ColumnName)
        {
            string sqlwhere = " SourceTag='人工' ";
            string sqlwhere_KuanXiangLeiXing="";
            //if (JingXiaoShang == "合计")
            //{ 
                
            //}

            string[] cNameArr=  ColumnName.Split(new char[] { '_' });
            string _KuanXiangLeiXing = cNameArr[0];
            string _fdate = cNameArr[1];
            string _tdate = cNameArr[2];

            if (_KuanXiangLeiXing == "其他")
            {
                sqlwhere_KuanXiangLeiXing = "  KuanXiangLeiXing not in('电汇','汇票','融资','调帐') ";
            }else if (_KuanXiangLeiXing == "合计")
            {
                sqlwhere_KuanXiangLeiXing = "  KuanXiangLeiXing<>'调帐' ";
            }
            else
            {
                sqlwhere_KuanXiangLeiXing = string.Format("   KuanXiangLeiXing='{0}'", _KuanXiangLeiXing);
            }
           
            //行合计
            
            
            if (JingXiaoShang == "合计:")
            {
                sqlwhere += string.Format(" and ShoukuanRiQi between '{0}' and '{1}'", _fdate, _tdate);
                sqlwhere += string.Format(" and {0}", sqlwhere_KuanXiangLeiXing);
                //营业经理权限处理
                string agentsqltemp = base.FTradeAgentPowerSqlWhere();
               
                sqlwhere +=string.IsNullOrEmpty(agentsqltemp)?"": string.Format(" and JingXiaoShang in ({0})", agentsqltemp);

                return FTradeCatheringController.GetHuKunJinE(sqlwhere);
            }
            ////列合计
            //if (_KuanXiangLeiXing == "合计")
            //{
            //    sqlwhere += string.Format(" ShoukuanRiQi between '{0}' and '{1}'", _fdate, _tdate);
            //    sqlwhere += string.Format(" and JingXiaoShang='{0}'", JingXiaoShang);
            //    sqlwhere += string.Format(" and KuanXiangLeiXing<>'调帐'", JingXiaoShang);
            //    return FTradeCatheringController.GetHuKunJinE(sqlwhere);
            //}

            sqlwhere += string.Format(" and ShoukuanRiQi between '{0}' and '{1}'", _fdate, _tdate);
            sqlwhere += string.Format(" and JingXiaoShang='{0}'", JingXiaoShang);
            sqlwhere += string.Format(" and {0}", sqlwhere_KuanXiangLeiXing);

           // object XX = FTradeCatheringController.GetHuKunJinE(sqlwhere);

            return FTradeCatheringController.GetHuKunJinE(sqlwhere);
        }
    }
}